package com.appkubes.mall.restful.application.goods.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.appkubes.mall.core.entity.*;
import com.appkubes.mall.core.service.impl.BaseMallGoodsServiceImpl;
import com.appkubes.mall.restful.application.goods.goods.dao.GoodsDao;
import com.appkubes.mall.restful.application.goods.goods.entity.dto.MallGoodDetailDto;
import com.appkubes.mall.restful.application.goods.goods.entity.model.GoodsIndexResult;
import com.appkubes.mall.restful.application.goods.goods.entity.model.GoodsUserModel;
import com.appkubes.mall.restful.application.goods.goods.entity.vo.GoodsesVO;
import com.appkubes.mall.restful.application.goods.goods.service.GoodsService;
import com.appkubes.mall.restful.application.goods.rent.service.GoodsRentService;
import com.appkubes.mall.restful.application.goods.sku.entity.dto.GoodsSkuDTO;
import com.appkubes.mall.restful.application.goods.sku.service.GoodsSpecificSkuService;
import com.appkubes.mall.restful.application.goods.sku.service.impl.GoodsSpecificServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Data
public class GoodsServiceImpl extends BaseMallGoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsRentService goodsRentService;

    @Autowired
    GoodsSpecificServiceImpl goodsSpecificServiceImpl;

    @Resource
    GoodsSpecificSkuService goodsSpecificSkuService;

    @Resource
    GoodsDao goodsDao;

    @Override
    public MallGoodDetailDto getRentGoods(String goodsKey) {
        MallGoodDetailDto mallGoodDetailDto = new MallGoodDetailDto();
        MallGoods hGoods = this.getById(goodsKey);
        MallGoodsRent rent = goodsRentService.findByKey(goodsKey);

        if (null != hGoods) {
            BeanUtils.copyProperties(hGoods, mallGoodDetailDto);
        }

        if (null != rent) {
            mallGoodDetailDto.setCostPrice(rent.getBuyOutAmount());
            mallGoodDetailDto.setBuyOutAmount(rent.getBuyOutAmount());
            mallGoodDetailDto.setRentInterestRate(rent.getRentInterestRate());
            mallGoodDetailDto.setCommisionRate(rent.getCommisionRate());
        } else {
            mallGoodDetailDto.setBuyOutAmount(hGoods.getCostPrice());
            mallGoodDetailDto.setRentInterestRate(BigDecimal.valueOf(0));
            mallGoodDetailDto.setCommisionRate(BigDecimal.valueOf(0));
        }

        mallGoodDetailDto.setPics(JSONObject.parseArray(hGoods.getPics(), String.class));
        return mallGoodDetailDto;
    }

    public String getSkuCode(String goodsKey, String attrName) {
        String[] strings = attrName.split(" ");
        MallGoodsSpecific specific = goodsSpecificServiceImpl.findByGoodsKey(goodsKey);

        List<GoodsSkuDTO> goodsSkuDTOList = JSONObject.parseArray(specific.getSpecificData(), GoodsSkuDTO.class);
        int dim1 = -1;
        int dim2 = -1;
        int dim3 = -1;
        int dim4 = -1;
        for (int i = 0; i < goodsSkuDTOList.size(); i++) {
            GoodsSkuDTO goodsSkuDTO = goodsSkuDTOList.get(i);
            if ("specific_dim1".equals(goodsSkuDTO.getSku())) {
                dim1 = i;
            }
            if ("specific_dim2".equals(goodsSkuDTO.getSku())) {
                dim2 = i;
            }
            if ("specific_dim3".equals(goodsSkuDTO.getSku())) {
                dim3 = i;
            }
            if ("specific_dim4".equals(goodsSkuDTO.getSku())) {
                dim4 = i;
            }
        }
        StringBuilder dimStr = new StringBuilder();
        if (dim1 > -1) dimStr.append(strings[dim1]);
        if (dim2 > -1) dimStr.append("-" + strings[dim2]);
        if (dim3 > -1) dimStr.append("-" + strings[dim3]);
        if (dim4 > -1) dimStr.append("-" + strings[dim4]);
        return dimStr.toString();
    }

    public MallGoods findBySkuCode(String goodsKey, String skuCode) {
        MallGoods mallGoods = this.getById(goodsKey);
        MallGoodsSpecificSku mallGoodsSpecificSku = goodsSpecificSkuService.find(goodsKey, skuCode);

        if (!StringUtils.isEmpty(mallGoodsSpecificSku.getSkuPic())) {
            mallGoods.setPic(mallGoodsSpecificSku.getSkuPic());
        }
        mallGoods.setPrice(mallGoodsSpecificSku.getSkuPrice());
        mallGoods.setOriginalPrice(mallGoodsSpecificSku.getSkuOriginalPrice());
        mallGoods.setStock(mallGoodsSpecificSku.getSkuStock());
        mallGoods.setWeight(mallGoodsSpecificSku.getSkuWeight());
        return mallGoods;
    }


    public int getGoodsOrderCount(String goodsKey) {
        return goodsDao.getOrderGoodsCount(goodsKey);
    }

    public List<GoodsUserModel> getGoodsUserModel(String goodsKey) {
        return goodsDao.getGoodsUserModel(goodsKey);
    }

    public List<MallGoods> getList(String queryStr, String orderBy, int page, int limit) {
        return goodsDao.getList(queryStr, orderBy, (page - 1) * limit, limit);
    }

    public List<GoodsIndexResult> getSaasGoodses(String queryStr, String orderBy, int page, int limit) {
        return goodsDao.getSaasGoodses(queryStr, orderBy, (page - 1) * limit, limit);
    }

    /**
     * 获取商品列表
     * @param goodsesVO
     * @return
     */
    public IPage getGoodses(GoodsesVO goodsesVO) {
        QueryWrapper<MallGoods> queryWrapper = new QueryWrapper();
        //判断是否有分类key,有分类key,按分类key查询
        if(!goodsesVO.getCatKey().equals("0")&&(goodsesVO.getCatKey()!=null)){
            queryWrapper.eq("cat_key", goodsesVO.getCatKey());
        }
        //判断有没有商品关键字，有的话按关键字查询
        if(goodsesVO.getGoodsName()!=null){
            queryWrapper.like("name", goodsesVO.getGoodsName());
        }
        IPage<MallGoods> page = new Page<>(goodsesVO.getPage(), goodsesVO.getSize());
        IPage<MallGoods> list = this.getBaseMapper().selectPage(page, queryWrapper);
        return list;
    }




}
